/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;
import util.HibernateUtil;
import java.util.List;
import pojo.Vaitro;
import org.hibernate.Query;
import org.hibernate.Session;
/**
 *
 * @author Jackhit
 */
public class VaiTroDAO {
    public List<Vaitro> layDanhSachVaiTro() {
        List<Vaitro> dsVaiTro = null;
         Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            String hql = "from Vaitro";
            Query query = session.createQuery(hql);
            dsVaiTro = query.list();
         }catch(Exception ex) {
            System.out.println(ex.getMessage());
        }finally {
            session.close();
        }
        return dsVaiTro;
    }

    public boolean themVaiTro(Vaitro vaiTro) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(vaiTro);
            session.getTransaction().commit();
            return true;
        }catch(Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        finally {
            session.close();
        }
    }

    public boolean capNhatVaiTro(Vaitro vaiTro) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(vaiTro);
            session.getTransaction().commit();
            return true;
        }catch(Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        finally {
            session.close();
        }
    }

    public boolean xoaVaiTro(Vaitro vaiTro) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(vaiTro);
            session.getTransaction().commit();
            return true;
        }catch(Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        finally {
            session.close();
        }
    }
}
